#!/usr/bin/env python
# encoding: utf-8
"""
sysintergratetest.py

Created by 黄 冬 on 2007-12-13.
Copyright (c) 2007 __MyCompanyName__. All rights reserved.
"""
from twisted.internet import reactor
import unittest
import logging.config
import socket, select

log = logging.getLogger('xbaytable.tests.sysintergratetest')
#logging.basicConfig(level=logging.ERROR)
#logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)

from xbaytable.conf import *
from xbaytable.utils.tableclient import *
from xbaytable.utils.tableserver import *
from xbaytable.utils.tablemsg import *
from messagetest import *

class TestSysintergrateProcess(ProcessMessage):
    """这是测试的消息处理，收包，确认发出的包都有正确的回复"""
    def process_table_set_resp(self,transport,command,sequence_id,msg):
        """处理process_table_set_resp回复"""
        status = binpack.unpackbody(command,msg)
        log.debug('client rev table_set_resp is %s'%status)
        transport.transport.loseConnection()

class TestSysintergrateClientProtocol(TestClientProtocol):
    """这是客户端协议的处理，主要是实现AuthOK之后要做的工作"""
    def AuthOK(self):
        """向服务器发出各种请求"""
        #发出set请求
        msg='abc'
        sequence_id = self.process.send_table_set(self,'a1','abc')
        self.sendwin.append(sequence_id)
        log.debug('send now socket_win is:%s'%len(self.sendwin))
        #发出append请求
        sequence_id = self.process.send_table_append(self,'a1','bbc')
        log.debug('send now socket_win is:%s'%len(self.sendwin))
        self.sendwin.append(sequence_id)
        #发出get请求
        sequence_id = self.process.send_table_get(self,'a1')
        log.debug('send now socket_win is:%s'%len(self.sendwin))
        self.sendwin.append(sequence_id)
        #发出del请求
        sequence_id = self.process.send_table_del(self,'a1')
        log.debug('send now socket_win is:%s'%len(self.sendwin))
        self.sendwin.append(sequence_id)

    def connectionMade(self):
        """链接建立，初始化测试数据"""
        TestClientProtocol.connectionMade(self)
        self.sendsetcount = 0
        self.sendnum = 1
        self.sendsetrep = 0

# class SysIntergrateTest(unittest.TestCase):
#     def setUp(self):
#         pass
# 
#     def tearDown(self):
#         pass
# 
#     def test_bind_table_server(self):
#         """测试连接到服务器"""
#         listen = reactor.listenTCP(TABLE_SERVER_PORT, TableServerFactory(TestSysintergrateProcess))
#         reactor.connectTCP('127.0.0.1', TABLE_SERVER_PORT, TableClientFactory(TestSysintergrateClientProtocol,TestSysintergrateProcess))
#         reactor.run()
# 
# def suite():
#     """集合测试用例"""
#     suite = unittest.TestSuite()
#     suite.addTest(unittest.makeSuite(SysIntergrateTest, 'test'))
#     return suite

if __name__ == '__main__':
    #unittest.main()
    listen = reactor.listenTCP(TABLE_SERVER_PORT, TableServerFactory(TestSysintergrateProcess))
    reactor.connectTCP('127.0.0.1', TABLE_SERVER_PORT, TableClientFactory(TestSysintergrateClientProtocol,TestSysintergrateProcess))
    reactor.run()